<template>
  <div class="goods-page">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="paramsData.query"
            class="input-with-select"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              @click="getGoodsList"
              slot="append"
              icon="el-icon-search"
            >
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <!-- 商品表格 -->
      <el-table :data="goods" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
        ></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="{ row }">
            <!-- 使用过滤器 -->
            {{ row.add_time | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini">
              编辑
            </el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramsData.pagenum"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="paramsData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      paramsData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表
      goods: [],
      //  总数量
      total: 0
    }
  },

  created() {
    this.getGoodsList()
  },

  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      const res = await this.$axios.get('goods', {
        params: this.paramsData
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.goods = res.data.goods
      this.total = res.data.total
    },
    // 选择下拉每页条数
    handleSizeChange(val) {
      this.paramsData.pagesize = val
      this.getGoodsList()
      // console.log(val)
    },
    // 页码发生变化
    handleCurrentChange(val) {
      this.paramsData.pagenum = val
      this.getGoodsList()
    }
  }
}
</script>

<style scoped lang='less'>
</style>
